#include "GraphicsWatermarkItem.h"
#include <GraphicsBaseItem.h>
#include <GraphicsUtil.h>
#include <QPainter>

GraphicsWatermarkItem::GraphicsWatermarkItem(GraphicsBaseItem* parent)
	:QGraphicsRectItem(parent)
	, m_parent(parent)
{
	assert(parent);
	m_watermarkImage = QImage(":/images/Grid-watermark-desktop.svg");
	//hide();
}

void GraphicsWatermarkItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
	painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
	const QSize itemSize = rect().size().toSize();
	const qreal ratio = (qreal)m_watermarkImage.width() / (qreal)m_watermarkImage.height();
	const QSize ratioSize = GraphicsUtil::getKeepRatioSize(itemSize.width(), itemSize.height(), ratio, true).toSize();

	QRect ratioRect = QRect(QPoint(0, 0), ratioSize);
	if (!m_parent->getItemData().IsOriginOnCenter()) {
		const QPoint center(itemSize.width() / 2, itemSize.height() / 2);
		ratioRect.moveCenter(center);
	}
	else {
		ratioRect.moveCenter(QPoint(0, 0));
	}
	QPoint start = ratioRect.topLeft();
	const QImage scaleImage = m_watermarkImage.scaled(ratioSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
	painter->drawImage(start, scaleImage);
}
